﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using EquipmentPanelTest.Data;

namespace EquipmentPanelTest.Controls
{
    /// <summary>
    /// Interaction logic for EquipmentGridCell.xaml
    /// </summary>
    public partial class EquipmentGridCell : UserControl, ICell, INotifyPropertyChanged
    {
        #region Dependency properties

        public static readonly DependencyProperty NormalBorderColorProperty =
            DependencyProperty.Register("NormalBorderColor", typeof(Color), typeof(EquipmentGridCell),
            new PropertyMetadata(Colors.Black));

        public Color NormalBorderColor
        {
            get
            {
                return (Color)GetValue(NormalBorderColorProperty);
            }
            set
            {
                SetValue(NormalBorderColorProperty, value);
            }
        }

        public static readonly DependencyProperty OccupiedBorderColorProperty =
            DependencyProperty.Register("OccupiedBorderColor", typeof(Color), typeof(EquipmentGridCell),
            new PropertyMetadata(Colors.Red));

        public Color OccupiedBorderColor
        {
            get
            {
                return (Color)GetValue(OccupiedBorderColorProperty);
            }
            set
            {
                SetValue(OccupiedBorderColorProperty, value);
            }
        }

        #endregion

        public bool IsOccupied
        {
            get
            {
                return isOccupied;
            }
            set
            {
                isOccupied = value;
                OnPropertyChanged("IsOccupied");
            }
        }

        public int Row { get; private set; }

        public int Column { get; private set; }

        public EquipmentGridCell(int row, int column)
        {
            InitializeComponent();

            Row = row;
            Column = column;

            SetBinding(BorderBrushProperty, new Binding()
            {
                Source = this,
                Path = new PropertyPath("IsOccupied"),
                Converter = new CachedBorderBrushConverter()
                {
                    Parent = this
                }
            });
        }

        public override string ToString()
        {
            return String.Format("{0}x{1}", Row, Column);
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

        #region Private fields

        private bool isOccupied;

        #endregion
    }

    public class CachedBorderBrushConverter : IValueConverter
    {
        private static Dictionary<Color, Brush> cache = new Dictionary<Color, Brush>();

        public EquipmentGridCell Parent { get; set; }

        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool isOccupied = (bool)value;
            Brush borderBrush = null;

            if (isOccupied)
            {
                var occupiedColor = Parent.OccupiedBorderColor;
                if (!cache.ContainsKey(occupiedColor))
                {
                    borderBrush = new SolidColorBrush(occupiedColor);
                    cache[occupiedColor] = borderBrush;
                }
                else
                {
                    borderBrush = cache[occupiedColor];
                }
            }
            else
            {
                var normalColor = Parent.NormalBorderColor;
                if (!cache.ContainsKey(normalColor))
                {
                    borderBrush = new SolidColorBrush(normalColor);
                    cache[normalColor] = borderBrush;
                }
                else
                {
                    borderBrush = cache[normalColor];
                }
            }

            return borderBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }

        #endregion
    }

}
